import { instantiate } from "cc";
import { Node } from "cc";
import { Prefab } from "cc";
import { NodePool } from "cc";
import { BaseMgr } from "../base/BaseMgr";

export class NodePoolMgr extends BaseMgr {
    static get inst() {
        return this.getInstance()
    }
    pools: { [key: string]: NodePool } = {};

    getOne(type: string, prefab: Prefab) {
        let pool = this.pools[type];
        if (!pool) {
            pool = new NodePool();
            this.pools[type] = pool;
        }
        if (pool.size() > 0) {
            return pool.get();
        } else {
            return instantiate(prefab);
        }
    }

    put(type: string, node: Node) {
        let pool = this.pools[type];
        if (!pool) {
            pool = new NodePool();
            this.pools[type] = pool;
        }
        pool.put(node);
    }

    destroy(): void {
        for (let i in this.pools) {
            const pool = this.pools[i]
            pool.clear();
        }
        this.pools = {};
    }
}